#!/bin/bash

# chkconfig: 2345 55 25

BASE=$(cd `dirname $0`; pwd)
PHP=/usr/local/php/bin/php
SRC=$BASE/modify_authorized_keys.php
SCRIPTNAME=$BASE/modify_authorized_keys
DESC="modify_authorized_keys_shell_running"
PID=`ps -ef | grep -v awk | awk '/'$DESC'/{print $2}'`

start()
{
   nohup $PHP $SRC $DESC > /dev/null 2>&1 &
}

stop()
{
   for var in $PID
   do
      result=`kill -KILL $var 2>&1`

      if [[ -n "$result" ]] ;then
         echo $result
         exit 1
      fi
   done
}

case "$1" in
   start)
      if [[ -n "$PID" ]] ;then
         echo "Has started!"
      else
         echo "Starting!"
         echo "."

         start

         echo "Successfully started!"
      fi
   ;;

   stop)
      if [[ -n "$PID" ]] ;then
         stop

         echo "Stopped running!"
      else
         echo "Not running!"
      fi
   ;;

   restart)
      if [[ -n "$PID" ]] ;then
         stop
      fi

      echo "Restarting!"
      echo "."

      start

      echo "Restarted!"
   ;;

   *)
      echo "Usage: start|stop|restart"
   ;;
esac

exit 0
